// packageA/pages/overtimeList/overtimeList.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
const util = require('../../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //传参
    paramObj:{},
    //列表
    list:[],
    //无数据
    showNothing:false,
    //搜索关键词
    searchValue:'',    
    //显示日历
    showDate: false,
    //开始日期
    startTime: util.timeFormatDay(new Date()),
    //结束日期
    endTime: util.timeFormatDay(new Date()),
    //日历范围默认今天-今天
    dateRange: [new Date().getTime(), new Date().getTime()],
    //最小值为60天前
    minDate: util.countPrevDay(new Date(),60),
    //当前用户
    user: null,
    //显示底部导航
    showTab: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      user: app.globalData.userInfo
    })

    if(options.paramObj){
      let paramObj=JSON.parse(options.paramObj);
      console.log('加班列表传参');
      console.log(paramObj);

      this.setData({
        paramObj: paramObj
      })

      if(paramObj.date){
        this.setData({
          startTime: paramObj.date,
          endTime: paramObj.date
        })
      }
    }

    //获取列表数据
    this.getList();
  },

  //获取列表数据
  getList(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });

    let param = {
      beginDate: this.data.startTime,
      endDate: this.data.endTime
    }

    //模糊查询
    if(this.data.searchValue){
      param.searchKey=this.data.searchValue;
    }

    CommonService.findWorkOverRecordList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result;
        console.log('加班列表');
        console.log(result);
        let current=new Date();
        
        result.forEach(item => {
          //加班时长
          item.self_hours=util.secondToFormat(item.duration*60*60);

          //现在之前的不能修改和删除
          let start=item.startTime.replaceAll('-','/');      
          start=new Date(start);
          if(start>current){
            item.self_isEdit=true;
          }
        });   

        this.setData({
          list: result
        })
        Toast.clear();
      }else{  
        this.setData({
          showNothing: true
        })
        Toast.clear();
      }
    }).catch((error) => {
      this.setData({
        showNothing: true
      })
      Toast.clear();
    })
  },

  //刷新列表和页码
  refreshListAndPage(){
    this.setData({
      list: [],
      showNothing: false
    })
    this.getList();
  },

  //获取搜索词
  onChangeSearch(e) {
    this.setData({
      searchValue: e.detail.value,
    })
  },

  //点击搜索
  onSearch(){
    this.refreshListAndPage();
  },

  //跳转添加
  goToAdd(){
    wx.navigateTo({
      url: "/packageA/pages/overtimeAdd/overtimeAdd"
    })  
  },

  //跳转编辑
  goToEdit({currentTarget}){
    let detail=currentTarget.dataset.detail;

    let obj={
      editInfo:{
        id: detail.id,
        personnel:[{
          imei: detail.imei
        }],
        startTime: detail.startTime,
        endTime: detail.endTime,
        duration: detail.duration,
        remark: detail.remark
      },
      name: detail.name,
      imei: detail.imei,
      self_hours: detail.self_hours
    }

    wx.navigateTo({
      url: "/packageA/pages/overtimeAdd/overtimeAdd?paramObj="+JSON.stringify(obj)
    })
  },

  //删除
  onDelete(e){
    Dialog.confirm({
      title: '确定删除此条加班记录吗',
      confirmButtonText:'确定',
      cancelButtonText:'不删除了'
    }).then(() => {
      let detail=e.currentTarget.dataset.detail;

      let param={
        id: detail.id,
        personnel:[{
          imei: detail.imei
        }],
        startTime: detail.startTime,
        endTime: detail.endTime,
        duration: detail.duration,
        remark: detail.remark
      }
      
      CommonService.deleteWorkOverRecord(param).then(res=>{
        if (res.statusCode == 200 && res.data.success == true){
          Toast.success('已删除');
          setTimeout(() => {
            //刷新列表
            this.refreshListAndPage();
          }, 1000);          
        }else{
          Toast.fail('操作失败');
        }
      }).catch((error) => {
        Toast.fail('操作失败');
      })

    }).catch(() => {
    });
  },

  //跳转步数统计
  goToStep({currentTarget}){
    let detail=currentTarget.dataset.detail;

    let obj={
      date: detail.startTime.substring(0,10),
      imei: detail.imei,
      nickName: detail.name,
      workOverStartTime: detail.startTime.substring(11,16),
      workOverEndTime: detail.endTime.substring(11,16)
    }

    wx.navigateTo({
      url: "/packageA/pages/stepChart/stepChart?paramObj="+JSON.stringify(obj)
    })
  },

  //跳转轨迹
  goToTrajectory({currentTarget}){
    let detail=currentTarget.dataset.detail;
    let paramObj={
      nickName: detail.name,
      lon: detail.lon,
      lat: detail.lat,
      imei: detail.imei,
      workOverStartTime: detail.startTime.substring(11,16),
      workOverEndTime: detail.endTime.substring(11,16)
    }
    wx.navigateTo({
			url: "/packageA/pages/staffTrajectory/staffTrajectory?paramObj="+JSON.stringify(paramObj)
		})
  },

  //显示日历
  onShowDate() {
    this.setData({
      showDate: true,
      showTab: false
    })
  },

  //隐藏日历
  onCloseDate() {
    this.setData({
      showDate: false,
      showTab: true
    })
  },

  //确认日期
  onConfirmDate(event) {
    const [start,end] = event.detail;
    this.setData({
      startTime: util.timeFormatDay(start),
      endTime: util.timeFormatDay(end)
    })
    this.onCloseDate();
    this.refreshListAndPage();
  },

  //查询今天
  onToday(){
    this.setData({
      startTime: util.timeFormatDay(new Date()),
      endTime: util.timeFormatDay(new Date())
    })
    this.refreshListAndPage();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   
  }
})